﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.IO;

namespace PlanningDepartment
{
   public class RowDetailsViewDefinition : TableViewDefinition
    {
        public override IRowView CreateViewUIElement(GridViewInfo viewInfo)
        {
            GridTableElement tableElement = (GridTableElement)base.CreateViewUIElement(viewInfo);
            tableElement.ViewElement.RowLayout = CreateRowLayout();
            return tableElement;
        }
        public override IGridRowLayout CreateRowLayout()
        { return new RowDetailsLayout(); }
    } 
    public class RowDetailsGrid : RadGridView 
    { 
        private GridViewDataColumn detailsColumn; 
        private int detailsRowHeight = 185; 
        public RowDetailsGrid() 
        { this.ViewDefinition = new RowDetailsViewDefinition(); } 
        public override string ThemeClassName 
        { get { return typeof(RadGridView).FullName; } set { } } 
        public GridViewDataColumn DetailsColumn
        { 
            get { return this.detailsColumn; } 
            set 
            { 
                if (detailsColumn != value) 
                { 
                    detailsColumn = value; 
                if (detailsColumn != null) 
                { 
                    detailsColumn.ReadOnly = true; 
                    detailsColumn.MinWidth = 0; 
                    detailsColumn.Width = 1; 
                } 
                    this.TableElement.UpdateView(); 
                } 
            } 
        } 
        [DefaultValue(185)]         
        public int DetailsRowHeight 
        { 
            get { return this.detailsRowHeight; } 
            set 
            { 
                if (detailsRowHeight != value) 
                { 
                    detailsRowHeight = value; 
                    TableElement.UpdateView(); 
                } 
            } 
        } 
        protected override void OnCreateRow(object sender, GridViewCreateRowEventArgs e) 
        { 
            if (e.RowType == typeof(GridDataRowElement)) 
            { 
                e.RowType = typeof(RowDetailsRowElement); 
            } 
            base.OnCreateRow(sender, e); 
        } 
    }
    public class RowDetailsRowElement : GridDataRowElement 
    { 
        protected override Type ThemeEffectiveType 
        { 
            get { return typeof(GridDataRowElement); } 
        } 
        public override void UpdateInfo() 
        { 
            if (!this.RowInfo.IsCurrent) 
            { this.RowInfo.Height = TableElement.RowHeight; } 
            else { this.RowInfo.Height = ((RowDetailsGrid)this.GridControl).DetailsRowHeight; } 
            base.UpdateInfo(); 
        } 
    }
    public class RowDetailsLayout : TableViewRowLayout
    {
        int systemWidth; SizeF availableSize;
        public override void InvalidateRenderColumns()
        {
            base.InvalidateRenderColumns(); systemWidth = 0;
            foreach (GridViewColumn column in this.RenderColumns)
            {
                if (!(column is GridViewDataColumn))
                {
                    systemWidth += column.Width + Owner.CellSpacing;
                }
            }
            if (systemWidth > 0)
            {
                systemWidth -= Owner.CellSpacing;
            }
        }
        public override SizeF MeasureRow(SizeF availableSize)
        {
            SizeF desiredSize = base.MeasureRow(availableSize);
            this.availableSize = availableSize; return desiredSize;
        }
        public override RectangleF ArrangeCell(RectangleF clientRect, GridCellElement cell)
        {
            RowDetailsGrid grid = (RowDetailsGrid)Owner.GridViewElement.GridControl;
            if (cell.ColumnInfo == grid.DetailsColumn)
            {
                cell.StretchHorizontally = true; cell.BypassLayoutPolicies = true;
                if (cell.RowElement.IsCurrent)
                { return new RectangleF(0, Owner.RowHeight, availableSize.Width - systemWidth, grid.DetailsRowHeight - Owner.RowHeight); }
                else { return RectangleF.Empty; }
            }
            if (cell is GridDataCellElement)
            { clientRect.Height = Owner.RowHeight; }
            return base.ArrangeCell(clientRect, cell);
        }
    }
}
